package com.mission.shaco.warehouse.models;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

import com.mission.shaco.warehouse.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ClientConfig {

    private static final String PREFERENCES_NAME = "com.zkc.smartsdk";

    private static Context mContext;

    private static SharedPreferences mSharedPreferences;

    private static Map<String, Object> configs = new HashMap<String, Object>();

    // private static PersistentCookieStore clientCookieStore;
    public static final String OPEN_SCAN = "open_scan";
    public static final String DATA_APPEND_ENTER = "data_append_enter";
    public static final String APPEND_RINGTONE = "append_ringtone";
    public static final String APPEND_VIBRATE = "append_vibate";
    public static final String CONTINUE_SCAN= "continue_scan";
    public static final String SCAN_REPEAT= "scan_repeat";
    public static final String RESET = "reset";
    public static Boolean TestModel = true;
    public static Boolean runDevelop = false;
    public static int isSpecial = 1;
    public static final String STOCK_OUT_SINGLE_HOST = "stockOutSingleHost";

    public static final String MULTI_LIST_ADAPTER_NAME = "adpater_multi";
    public static final String SINGLE_LIST_ADAPTER_NAME = "adpater_single";
    public static String HOST_IP = "http://112.74.66.161/";
    public static String DEVELOP_HOST_IP = "http://192.168.45.190/";
    public static String APP_IP =  "http://26.16.223.240:8087/";
    public static final String APP_CHECK_URL = "http://112.74.66.161/old/develop";
    public static final String HOST_WEB_URL = "ICNEW/index.php/";


    public static final String FILES_PATH = "/data/data/com.mission.shaco.warehouse/files/";
    public static final String JSON_GET_OPERATOR_PATH = "OrderPick/getPickList";
    public static final String JSON_SINGLE_PIKING_PATH =  "OrderPick/getSingleList";
    public static final String JSON_TRACKING_CONFIRM =  "OrderPick/purchaseSign";
    public static final String JSON_MULTI_PIKING_PATH =  "OrderPick/getMultiList";
    public static final String JSON_SINGLE_COMPLETE_PATH =  "OrderPick/singlePickComplete";
    public static final String JSON_MULTI_COMPLETE_PATH =  "OrderPick/multiPickComplete";
    public static final String JOSN_GET_REST_TODAY_PATH =  "OrderPick/nopickStatistics";
    public static final String JOSN_CHECK_GRID_NUMBER_PATH =  "OrderPick/checkGridnumberRepeat";
    public static final String JOSN_LOGIN_PATH =  "OrderPick/login";
    public static final String JOSN_GET_PRODUCT_STOCK =  "Stock/getProductInfo";
    public static final String JOSN_UPDATE_PRODUCT_QTY =  "Stock/updateQty";
    public static final String JOSN_GET_STOCK_INFO =  "Stock/getStockInfo";
    public static final String JOSN_UPDATE_PRODUCT_STOCK =  "Stock/setStock";
    public static final String JSON_CANCEL_PATH =  "OrderPick/cancelPick";
    public static final String JSON_GET_STOCK_IN_INFO =  "OrderPick/getPrepareOnsaleCount";
    public static final String JSON_GET_STOCK_IN_COMPLETE =  "OrderPick/completeOnsale";
    public static final String JSON_GET_VERSION_PATH =  "update.xml";
    public static final Long CHECK_TIME_SPACE = 700L;

    public static final int RESPONSE_STATUS_SUCCESS = 1;
    public static final int RESPONSE_STATUS_WARNING = 2;
    public static final int RESPONSE_STATUS_FAILED = -1;

    public static final int ORDER_PICK_TYPE_SINGLE = 10;
    public static final int ORDER_PICK_TYPE_SINGLE_MULTI = 20;
    public static final int ORDER_PICK_TYPE_MULTI_MULTI = 30;




    public static final int ITEM_ERROR_OUT_OF_STOCK = 1;
    public static final int ITEM_ERROR_WRONG_STOCK = 2;
    public static final int ITEM_ERROR_BREAK_PRODUCT = 3;

    private static void initDefaultValue() {

    }

    public static String getHostUrl(String path){
        String host = HOST_IP;
        if(runDevelop){
            host = DEVELOP_HOST_IP;
        }
        return host + HOST_WEB_URL + path;
    }

    public static String getAppUrl(String path){
        return APP_IP + path;
    }


    public static void init(Context context) {
        mContext = context;
        // clientCookieStore=new PersistentCookieStore(mContext);
        getSharedPreferences();
        if (mSharedPreferences != null) {
            initDefaultValue();
            try {
                Map<String, Object> maps = (Map<String, Object>) mSharedPreferences
                        .getAll();
                for (Entry<String, Object> properites : maps.entrySet()) {
                    configs.put(properites.getKey(), properites.getValue());
                }
            } catch (Exception e) {
                Log.e("init error:", e.toString());
            }
        }
    }

    public static String getStockStatusLabel(String status){
        switch (status){
            case "0" : return "正常";
            case "1" : return "停产";
            case "2" : return "下架";
            case "3" : return "卖完就下架";
            case "4" : return "暂时缺货";
            case "5" : return "清库存";
        }
        return "";
    }

    public static int getStockStatusColor(String status){
        switch (status){
            case "0" : return R.color.stockStatusNormal;
            case "1" : return R.color.stockStatusNormal;
            case "2" : return R.color.stockStatusXiaJia;
            case "3" : return R.color.stockStatusQingKu;
            case "4" : return R.color.stockStatusNormal;
            case "5" : return R.color.stockStatusQingKu;
        }
        return R.color.stockStatusNormal;
    }

    /**
     * 获取分享参数
     *
     * @return
     */
    private static synchronized SharedPreferences getSharedPreferences() {
        if (mSharedPreferences == null) {
            if (mContext != null) {
                mSharedPreferences = mContext.getSharedPreferences(
                        PREFERENCES_NAME, Context.MODE_PRIVATE);
            } else {
                Log.e("SharedPreferencesNUll", "getSharedPreferences error:mContext is null");
            }
        }
        return mSharedPreferences;
    }

    public static boolean hasValue(String key) {
        if (mSharedPreferences != null) {
            return mSharedPreferences.contains(key);
        }
        return false;
    }

    public static String getString(String key) {
        return getString(key, "");
    }

    public static String getString(String key, String defaultValue) {
        String msg = defaultValue;
        try {
            Object obj = configs.get(key);
            msg = obj == null ? "" : obj.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static int getInt(String key) {
        return getInt(key, 0);
    }

    public static int getInt(String key, int defaultValue) {
        int msg = defaultValue;
        try {
            Object obj = configs.get(key);
            if (obj != null) {
                if (obj instanceof Integer) {
                    msg = (Integer) obj;
                } else {
                    msg = Integer.valueOf(obj.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static long getLong(String key, long defaultValue) {
        long msg = defaultValue;
        try {
            Object obj = configs.get(key);
            if (obj != null) {
                if (obj instanceof Long) {
                    msg = (Long) obj;
                } else {
                    msg = Long.valueOf(obj.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static long getLong(String key) {
        return getLong(key, 0);
    }

    public static float getFloat(String key, float defaultValue) {
        float msg = defaultValue;
        try {
            Object obj = configs.get(key);
            if (obj != null) {
                if (obj instanceof Float) {
                    msg = (Float) obj;
                } else {
                    msg = Float.valueOf(obj.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static float getFloat(String key) {
        return getFloat(key, 0);
    }

    public static Double getDouble(String key, double defaultValue) {
        double msg = defaultValue;
        try {
            Object obj = configs.get(key);
            if (obj != null) {
                if (obj instanceof Double) {
                    msg = (Double) obj;
                } else {
                    msg = Double.valueOf(obj.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static Double getDouble(String key) {
        return getDouble(key, 0);
    }

    public static Boolean getBoolean(String key, boolean defaultValue) {
        boolean msg = defaultValue;
        try {
            Object obj = configs.get(key);
            if (obj != null) {
                msg = Boolean.valueOf(obj.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static Boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public static void setValue(String key, Object value) {
        try {
            configs.put(key, value);
            SharedPreferences mSharedPreferences = getSharedPreferences();
            Editor edit = mSharedPreferences.edit();
            if (null == value) {
                edit.remove(key);
            } else {
                if (value instanceof String || value instanceof Double) {
                    edit.putString(key, value.toString());
                } else if (value instanceof Integer) {
                    edit.putInt(key, (Integer) value);
                } else if (value instanceof Long) {
                    edit.putLong(key, (Long) value);
                } else if (value instanceof Float) {
                    edit.putFloat(key, (Float) value);
                } else if (value instanceof Boolean) {
                    edit.putBoolean(key, (Boolean) value);
                }
            }
            edit.commit();
        } catch (Exception e) {
            Log.e("setValue error:", e.toString());
        }
    }

    // public static PersistentCookieStore getCookieStore(){
    // return clientCookieStore;
    // }

    public static void clearAccount() {

    }

    public static JSONObject getJSONObject(String key) throws JSONException {
        if(TextUtils.isEmpty(getString(key))){
            return null;
        }else {
            return new JSONObject(getString(key));
        }
    }

    public static void setJSONObject(String key,JSONObject json){
        setValue(key,json.toString());
    }

}
